package com.wl.application.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wl.application.common.all.Const;
import com.wl.application.entity.Menu;
import com.wl.application.entity.vo.MenuVo;
import com.wl.application.mapper.MenuMapper;
import com.wl.application.services.MenuService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wl
 * @version 1.0
 * @date 2022/6/22
 * @description:
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {


    @Override
    public boolean updateState(Integer id) {
        Menu menu1 = baseMapper.selectById(id);
        closeChildCode(menu1, null);
        return false;
    }

    private void closeChildCode(Menu menu1, Integer parent) {
        //父节点为空是才需要给值
        //是修改的父节点 获取父节点的状态
        if (parent == null) {
            if (menu1.getMstate().equals(Const.NO_ACTIVATE)) {
                menu1.setMstate(Const.ACTIVATE);
                parent=Const.ACTIVATE;
            } else {
                menu1.setMstate(Const.NO_ACTIVATE);
                parent=Const.NO_ACTIVATE;
            }
        } else {
            //是修改节点的子孙节点 和父节点一直
            menu1.setMstate(parent);
        }
        //在数据库中修改
        baseMapper.updateById(menu1);
        //获取以当前节点为父节点的所有子节点
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("pid", menu1.getId());
        List<Menu> list = baseMapper.selectList(queryWrapper);
        //当前节点有子节点,修改子节点的状态和父节点保持一直
        if (list != null) {
            for (Menu menu : list) {
                //修改子节点
                closeChildCode(menu, parent);
            }
        } else {
            return;
        }
    }

    @Override
    public MenuVo getMenuById(Integer id) {
        return baseMapper.getMenuById(id);
    }

    @Override
    public List<Map<String, Object>> nodeList() {

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.orderByAsc("pid");
        wrapper.orderByAsc("msort");
        //1.查询所有的菜单信息
        List<Menu> menus = baseMapper.selectList(wrapper);
        List<Map<String, Object>> maps = toJsonNodeList(menus);
        return maps;
    }


    private List<Map<String, Object>> toJsonNodeList(List<Menu> menuList) {
        List<Map<String, Object>> maps = new ArrayList<>();
        for (Menu menu : menuList) {
            //当前元素是根元素
            if (menu.getPid() == -1) {
                Map<String, Object> parentMaps = new HashMap<>();
                parentMaps.put("id", menu.getId());
                parentMaps.put("pid", menu.getPid());
                parentMaps.put("sort", menu.getMsort());
                parentMaps.put("type", menu.getMtype());
                parentMaps.put("url", menu.getUrl());
                parentMaps.put("tag", menu.getMtag());
                parentMaps.put("mName", menu.getMname());
                parentMaps.put("mstate", menu.getMstate());
                parentMaps.put("children", new ArrayList<>());
                //将当前的元素添加到返回的集合中
                maps.add(parentMaps);
            } else {
                //在存入的总集合中递归添加当前元素
                addThisChildren(maps, menu);
            }
        }
        //获取了所有的根元素
        return maps;
    }

    void addThisChildren(List<Map<String, Object>> maps, Menu NewMap) {
        //当前元素在此maps中的节点中
        for (Map<String, Object> map : maps) {
            if (map.get("id") == NewMap.getPid()) {
                if (map.get("children") == null) {
                    map.put("children", new ArrayList<>());
                }
                List children = (List) map.get("children");
                Map<String, Object> parentMaps = new HashMap<>();
                parentMaps.put("id", NewMap.getId());
                parentMaps.put("mName", NewMap.getMname());
                parentMaps.put("pid", NewMap.getPid());
                parentMaps.put("sort", NewMap.getMsort());
                parentMaps.put("type", NewMap.getMtype());
                parentMaps.put("tag", NewMap.getMtag());
                parentMaps.put("url", NewMap.getUrl());
                parentMaps.put("mstate", NewMap.getMstate());
                parentMaps.put("children", new ArrayList<>());
                children.add(parentMaps);
                return;
            }
        }
        for (Map<String, Object> map : maps) {
            if ((List<Map<String, Object>>) map.get("children") != null) {
                addThisChildren((List<Map<String, Object>>) map.get("children"), NewMap);
            }
        }
    }
}
